package win.smartown.app.request;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.GET;
import retrofit2.http.Path;
import rx.Observable;
import win.smartown.app.gank.entity.Data;
import win.smartown.app.gank.entity.GankResponse;

/**
 * Created by smartown on 2017/3/18.
 * <br>
 * Description:
 */
public class RequestManager {

    private static OkHttpClient okHttpClient;

    public static synchronized OkHttpClient getOkHttpClient() {
        if (okHttpClient == null) {
            okHttpClient = new OkHttpClient.Builder()
                    .writeTimeout(10, TimeUnit.SECONDS)
                    .readTimeout(10, TimeUnit.SECONDS)
                    .addInterceptor(new Interceptor() {
                        @Override
                        public Response intercept(Chain chain) throws IOException {
                            return chain.proceed(chain.request());
                        }
                    })
                    .build();
        }
        return okHttpClient;
    }

    private static Retrofit newRetrofit(String baseUrl) {
        return new Retrofit.Builder()
                .client(RequestManager.getOkHttpClient())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .baseUrl(baseUrl)
                .build();
    }

    public static class GankManager {

        private final static String baseUrl = "http://gank.io/api/";
        private static Retrofit retrofit;

        private static synchronized Retrofit getRetrofit() {
            if (retrofit == null) {
                retrofit = RequestManager.newRetrofit(baseUrl);
            }
            return retrofit;
        }

        private static GankService getGankService() {
            return getRetrofit().create(GankService.class);
        }

        /**
         * 分类数据
         *
         * @param type 数据类型： 福利 | Android | iOS | 休息视频 | 拓展资源 | 前端 | all
         * @param size 请求个数： 数字，大于0
         * @param page 第几页：数字，大于0
         * @return
         */
        public static Observable<GankResponse<List<Data>>> getDataByType(String type, int size, int page) {
            return getGankService().getDataByType(type, size + "", page + "");
        }

        /**
         * 获取发过干货日期
         *
         * @return
         */
        public static Observable<GankResponse<List<String>>> getDayHistory() {
            return getGankService().getDayHistory();
        }

        /**
         * 每日数据
         *
         * @param year
         * @param month
         * @param day
         * @return
         */
        public static Observable<GankResponse<HashMap<String, List<Data>>>> getDayData(int year, int month, int day) {
            return getGankService().getDayData(year + "", formatNumber(month), formatNumber(day));
        }

        private static String formatNumber(int number) {
            return String.format("%02d", number);
        }

    }

    private interface GankService {

        @GET("data/{type}/{size}/{page}")
        Observable<GankResponse<List<Data>>> getDataByType(@Path("type") String type, @Path("size") String size, @Path("page") String page);

        @GET("day/history")
        Observable<GankResponse<List<String>>> getDayHistory();

        @GET("day/{y}/{m}/{d}")
        Observable<GankResponse<HashMap<String, List<Data>>>> getDayData(@Path("y") String year, @Path("m") String month, @Path("d") String day);

    }

}
